﻿Imports System.IO
Imports System.Windows.Forms
Imports System.Windows.Forms.TreeView

Namespace Project
    Public Class Project

        ' Winforms TreeView for the Project Explorer
        Public Property FileTreeView As ProjectFileTree

        ' Is the project saved?
        Public Property Saved As Boolean

        ' Simple list of each file in the project
        Private _filelist As ProjectFileList
        ReadOnly Property FileList As ProjectFileList
            Get
                Return _filelist
            End Get
        End Property

        ' Name of the Project
        Private _projectname As String
        ReadOnly Property ProjectName As String
            Get
                Return _projectname
            End Get
        End Property

        ReadOnly Property ProjectFileName As String
            Get
                Return ProjectFolder + ProjectName + ".axproj"
            End Get
        End Property

        ' Root folder of the Project (UDK Directory)
        Private _projectfolder As String
        ReadOnly Property ProjectFolder As String
            Get
                Return _projectfolder
            End Get
        End Property

        ' Folder containing actual Source Code (UnrealScript)
        ReadOnly Property DevelopmentFolder As String
            Get
                Return _projectfolder + "Development\Src\"
            End Get
        End Property

        Public Function NodeFullPathToFullName(ByRef fullpath As String) As String
            ' hmmm...
            Return DevelopmentFolder + fullpath.Replace(_projectname + "\", "")
        End Function

        Public Sub New(ByRef name As String, ByRef udkdir As String, Optional ByRef filebrowser_regex As String = Globals.kProjectFileRegex)

            _projectname = name
            _projectfolder = udkdir

            ' By default, we assume we're not saved
            Saved = False

            ' Add the path separator if needed
            If Not _projectfolder.EndsWith("\") Then
                _projectfolder += "\"
            End If

            ' Create our list of project files
            _filelist = New ProjectFileList(Me, DevelopmentFolder, filebrowser_regex)
        End Sub

        Public Shared Function OpenProject(ByRef path As String) As Project
            ' Return a Project object from a file path 
            Dim projectsettings As New Settings(path, "Project")
            projectsettings.ReadFromXml()

            Dim projectname As String = projectsettings.GetTrait("Name", "Corrupt Project File")
            Dim folder As String = projectsettings.GetTrait("RootFolder", "Bad Root Directory")

            Dim project As New Project(projectname, folder, Globals.kProjectFileRegex)

            ' TODO: Load Database for Actor Parser

            Return project
        End Function

        Public Sub AddFile(ByRef folder As String, ByRef filename As String)
            For i As Integer = 0 To FileTreeView.FileTree.Nodes.Count - 1
                Dim node = FileTreeView.FileTree.Nodes(i)
                If NodeFullPathToFullName(node.FullPath) = folder Then
                    node.Nodes.Add(filename)
                End If
            Next
        End Sub

        Public Sub SaveProject()
            ' Save the Project to a XML file
            Dim projectsettings As New Settings(ProjectFolder + ProjectName + ".axproj", "Project")

            projectsettings.SetTrait("First", ProjectName)
            projectsettings.SetTrait("Name", ProjectName)
            projectsettings.SetTrait("RootFolder", ProjectFolder)
            projectsettings.WriteToXml()

            ' TODO: Save settings per file...

            ' TODO: Save Database for Actor Parser
        End Sub

    End Class
End Namespace